package com.angel.architecture.initializer.injector.impl;

import java.lang.reflect.Field;

import org.apache.log4j.Logger;

import com.angel.architecture.annotations.services.Service;
import com.angel.architecture.initializer.injector.Injector;
import com.angel.architecture.services.locator.ServiceLocator;
import com.angel.common.helpers.ReflectionHelper;

/**
 *
 * @author William
 *
 */
public class ServiceInjector extends Injector {

	private static final Logger LOGGER = Logger.getLogger(ServiceInjector.class);

	@Override
	public boolean applies(Field field) {
	    Class<?> fieldType = field.getType();
	    return fieldType.isAnnotationPresent(Service.class);
	}

	@Override
	protected void doInject(Field field, Object object) {
	    Class<?> fieldType = field.getType();
	    LOGGER.debug("Injecting service " + fieldType.getSimpleName() + " on object " + object);
	    field.setAccessible(true);
	    Object service = ServiceLocator.locate(fieldType, true);
	    ReflectionHelper.setFieldValue(object, field, service);
	    //writeField(object, field, ServiceLocator.locate(fieldType, true));
	}

}
